//
//  MAModifyView.m
//  Stock_Pro_ax
//
//  Created by gaotime on 15/10/12.
//  Copyright © 2015年 gaotime. All rights reserved.
//

#import "MAModifyView.h"
#import "config.h"
#import "AppInfo.h"
#import "ColorTools.h"
#import "HZAction.h"

#import "QuoteLvOper.h"
extern QUOTELV_OPER *g_pQuoteOper;

@implementation MAModifyView

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  self.backgroundColor = [UIColor grayColor];
  if (self) {
    self.backgroundColor = _rgb2uic(0xeae9ed, 1);
    pTitleLabel = [self createLabel:@"均线设置" withFrame:CGRectMake(0, 0, self.bounds.size.width, 40) withAlignment:NSTextAlignmentCenter withFont:[UIFont systemFontOfSize:20] withColor:[UIColor lightGrayColor]];
//    UIImage *img = [UIImage imageNamed:@"home_topBG.png"];
//    CGSize imgSize = pTitleLabel.frame.size;
//    
//    UIGraphicsBeginImageContext( imgSize );
//    [img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
//    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    pTitleLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
    
    [self addSubview:pTitleLabel];
    
    pLineT = [[UILabel alloc]initWithFrame:CGRectMake(0, 40,self.bounds.size.width,2)];
    pLineT.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:pLineT];
    
//    pLeftLabel0 = [self createLabel:@"K线的第一条" withFrame:CGRectMake(10, 60, 100, 20) withAlignment:NSTextAlignmentRight withFont:[UIFont systemFontOfSize:15] withColor:[UIColor blackColor]];
//    [self addSubview:pLeftLabel0];
    
    // Initialization code
    pMaField0 = [self createTextField:@"MA5" withFrame:CGRectMake(30, 60, 60, 30) withTextColor:_rgb2uic(0xff51f9, 1)];
    [self addSubview:pMaField0];
    
    pRightLabel0 = [self createLabel:@"日的移动平均" withFrame:CGRectMake(100, 65, 110, 20) withAlignment:NSTextAlignmentLeft withFont:[UIFont systemFontOfSize:16] withColor:_rgb2uic(0xff51f9, 1)];
    [self addSubview:pRightLabel0];
    
//    pLeftLabel1 = [self createLabel:@"K线的第二条" withFrame:CGRectMake(10, 90, 100, 20) withAlignment:NSTextAlignmentRight withFont:[UIFont systemFontOfSize:15] withColor:[UIColor blackColor]];
//    [self addSubview:pLeftLabel1];
    
    // Initialization code
    pMaField1 = [self createTextField:@"MA10"
                                 withFrame:CGRectMake(30, 100, 60, 30) withTextColor:_rgb2uic(0xf6bb42, 1)];
    [self addSubview:pMaField1];
    
    pRightLabel1 = [self createLabel:@"日的移动平均" withFrame:CGRectMake(100, 105, 110, 20) withAlignment:NSTextAlignmentLeft withFont:[UIFont systemFontOfSize:16] withColor:_rgb2uic(0xf6bb42, 1)];
    [self addSubview:pRightLabel1];
    
//    pLeftLabel2 = [self createLabel:@"K线的第三条" withFrame:CGRectMake(10, 120, 100, 20) withAlignment:NSTextAlignmentRight withFont:[UIFont systemFontOfSize:15] withColor:[UIColor blackColor]];
//    [self addSubview:pLeftLabel2];
    
    // Initialization code
    pMaField2 = [self createTextField:@"MA20"
                                 withFrame:CGRectMake(30, 140, 60, 30) withTextColor:_rgb2uic(0x5d9cec, 1)];
    [self addSubview:pMaField2];
    
    pRightLabel2 = [self createLabel:@"日的移动平均" withFrame:CGRectMake(100, 145, 140, 20) withAlignment:NSTextAlignmentLeft withFont:[UIFont systemFontOfSize:16] withColor:_rgb2uic(0x5d9cec, 1)];
    [self addSubview:pRightLabel2];
    
    // Initialization code
    pMaField3 = [self createTextField:@"MA60"
                            withFrame:CGRectMake(30, 180, 60, 30) withTextColor:_rgb2uic(0x888888, 1)];
    [self addSubview:pMaField3];
    
    pRightLabel3 = [self createLabel:@"日的移动平均" withFrame:CGRectMake(100, 185, 110, 20) withAlignment:NSTextAlignmentLeft withFont:[UIFont systemFontOfSize:16] withColor:_rgb2uic(0x888888, 1)];
    [self addSubview:pRightLabel3];
    
    UILabel *pLineV = [[UILabel alloc]initWithFrame:CGRectMake(0, 225, 240,1)];
    pLineV.backgroundColor = [UIColor lightGrayColor];
    
    UILabel *pLineH = [[UILabel alloc]initWithFrame:CGRectMake(120, 225, 1,40)];
    pLineH.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:pLineV];
    [self addSubview:pLineH];
    
    pCancelBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pCancelBtn.frame = CGRectMake(120, 226, 120, 39);
    pCancelBtn.tag = 0;
    [pCancelBtn addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
    [pCancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [pCancelBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [pCancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//    pCancelBtn.hidden = YES;
    
    pAgreeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pAgreeBtn.frame = CGRectMake(0, 226, 120, 39);
    pAgreeBtn.tag = 1;
    [pAgreeBtn addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
    [pAgreeBtn setTitle:@"确定" forState:UIControlStateNormal];
    [pAgreeBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [pAgreeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    
//    pLeftLabel3 = [self createLabel:@"K线的第四条" withFrame:CGRectMake(10, 150, 100, 20) withAlignment:NSTextAlignmentRight withFont:[UIFont systemFontOfSize:15] withColor:[UIColor blackColor]];
//    [self addSubview:pLeftLabel3];
    
//    pBackLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 190, self.bounds.size.width, 50)];
//    imgSize = pBackLabel.frame.size;
//    
//    UIGraphicsBeginImageContext(imgSize);
//    [img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
//    newImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    pBackLabel.backgroundColor = [UIColor colorWithPatternImage:newImage];
//    [self addSubview:pBackLabel];
    
//    pAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    pAgreeBtn.frame = CGRectMake(30, 210, 80, 30);
//    pAgreeBtn.tag = 0;
//    [pAgreeBtn.layer setMasksToBounds:YES];
//    [pAgreeBtn.layer setCornerRadius:3.0]; //设置矩形四个圆角半径
//    [pAgreeBtn.layer setBorderWidth:1.0];
//    [pAgreeBtn setTitle:@"确定" forState:UIControlStateNormal];
//    pAgreeBtn.backgroundColor = _rgb2uic(0xe44d42,1.0);
//    pAgreeBtn.layer.borderColor=_rgb2uic(0xe44d42,1.0).CGColor;
//    [pAgreeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
////    self.pAgreeBtn.enabled = NO;
//    [pAgreeBtn addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:pAgreeBtn];
//    
//    pCancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    pCancelBtn.frame = CGRectMake(self.bounds.size.width-110, 210, 80, 30);
//    pCancelBtn.tag = 1;
//    [pCancelBtn.layer setMasksToBounds:YES];
//    [pCancelBtn.layer setCornerRadius:3.0]; //设置矩形四个圆角半径
//    //边框宽度
//    [pCancelBtn.layer setBorderWidth:1.0];
//    //  pResetButton.backgroundColor = [UIColor whiteColor];
//    pCancelBtn.backgroundColor = _rgb2uic(0x2594e2,1.0);
//    pCancelBtn.layer.borderColor=_rgb2uic(0x2594e2,1.0).CGColor;
//    [pCancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [pCancelBtn setTitle:@"取消" forState:UIControlStateNormal];
//    [pCancelBtn setBackgroundImage:[UIImage imageNamed:@"button_bg.png"] forState:UIControlStateNormal];
//    [pCancelBtn addTarget:self action:@selector(buttonActive:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:pCancelBtn];
    
    nOffset = 0;
    pScrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    
    pForegroundView = [[ForegroundView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    pForegroundView.backgroundColor = [UIColor clearColor];
    [pForegroundView setHiddenDelegate:self];
    [pForegroundView setHidden:YES];
  }
  return self;
}

// Override父类的layoutSubviews方法
//- (void)layoutSubviews {
//  [super layoutSubviews];     // 当override父类的方法时，要注意一下是否需要调用父类的该方法
//  
//  for (UIView* view in self.subviews) {
//    // 搜索AlertView底部的按钮，然后将其位置下移
//    // IOS5以前按钮类是UIButton, IOS5里该按钮类是UIThreePartButton
//    if ([view isKindOfClass:[UIButton class]] ||
//        [view isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
//      CGRect btnBounds = view.frame;
//      btnBounds.origin.y = self.pMaField2.frame.origin.y + self.pMaField2.frame.size.height + 7;
//      view.frame = btnBounds;
//    }
//  }
//  
//  // 定义AlertView的大小
//  CGRect bounds = self.frame;
////  bounds.size.width = 400;
//  bounds.size.height = 260;
//  self.frame = bounds;
//}

- (UITextField*)createTextField:(NSString*)placeholder withFrame:(CGRect)frame withTextColor:(UIColor *)color{
  UITextField* field = [[UITextField alloc] initWithFrame:frame];
//  field.placeholder = placeholder;
  field.backgroundColor = [UIColor whiteColor];
  field.textColor = color;
  field.textAlignment = NSTextAlignmentCenter;
//  field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  field.delegate = self;
//  pTextAccount.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//  pTextAccount.borderStyle = UITextBorderStyleRoundedRect;
//  pTextAccount.keyboardAppearance = UIKeyboardAppearanceDefault;
    field.returnKeyType = UIReturnKeyDone;
//  pTextAccount.clearButtonMode = UITextFieldViewModeWhileEditing;
//  pTextAccount.autocorrectionType = UITextAutocorrectionTypeNo;
//  [pTextAccount setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
  
  return field;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [pForegroundView setHidden:YES];
  [self rePoint];
  [textField resignFirstResponder];
  return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
  if (nOffset == 0) {
    [UIView beginAnimations:@"showKeyboardAnimation" context:nil];
    [UIView setAnimationDuration:0.30];
    
//    if (textField != pFundField) {
      nOffset = 100;
//    }
    
    pScrollView.frame = CGRectMake(pScrollView.frame.origin.x, pScrollView.frame.origin.y- nOffset, pScrollView.frame.size.width, pScrollView.frame.size.height);
    [UIView commitAnimations];
    [pForegroundView setHidden:NO];
    [self sendSubviewToBack:pForegroundView];
    
  }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
  
//  if (textField == pFundField) {
//    if ([pFundField.text length] == 6) {
//      char *pStock = [pFundField.text UTF8String];
//      char pp[16];
//      strcpy(pp,pStock);
//      [pFundField setText:[NSString stringWithUTF8String:pp]];
//    }
//  }
}

- (void)rePoint
{
  if (nOffset!= 0) {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.30];
    CGRect rect = pScrollView.frame;
    rect.origin.y += nOffset;
    pScrollView.frame = rect;
    
    [UIView commitAnimations];
  }
  nOffset = 0;
}

- (UILabel*)createLabel:(NSString*)text withFrame:(CGRect)frame withAlignment:(NSTextAlignment)alignment withFont:(UIFont*)font withColor:(UIColor*)color{
  UILabel *label = [[UILabel alloc]initWithFrame:frame];
  label.textAlignment = alignment;
  label.text = text;
  label.font = font;
  label.textColor = color;
  
  return label;
}

- (void)buttonActive:(UIButton *)sender {
    int aTag =sender.tag;
  switch (aTag) {
    case 1:
      {
          [self saveData];
          [self.parent upToParentWithObjc:self withType:KMSKlineChartViewType withParamter:@"Save_MA"];
      }
        
      break;
    case 0:
      break;
    default:
      break;
  }
  [self setHidden:YES];
}

+ (void)readConfigData
{
    ReadConfig();
    char *pMA1 = GetConfig(CONFIG_MA1);
    char *pMA2 = GetConfig(CONFIG_MA2);
    char *pMA3 = GetConfig(CONFIG_MA3);
    char *pMA4 = GetConfig(CONFIG_MA4);
    //修改均线
    QUOTELV_K_INDEX *kIndex = g_pQuoteOper->KIndex;
    kIndex->PAVG_N1 = atoi(pMA1);
    kIndex->PAVG_N2 = atoi(pMA2);
    kIndex->PAVG_N3 = atoi(pMA3);
    kIndex->PAVG_N4 = atoi(pMA4);
}

- (void)readData{
  ReadConfig();
  char *pMA1 = GetConfig(CONFIG_MA1);
  char *pMA2 = GetConfig(CONFIG_MA2);
  char *pMA3 = GetConfig(CONFIG_MA3);
  char *pMA4 = GetConfig(CONFIG_MA4);
  
  NSString* pMa1 = @"5";
  NSString* pMa2 = @"10";
  NSString* pMa3 = @"20";
  NSString* pMa4 = @"60";
  if (pMA1 != NULL && pMA2 != NULL && pMA3 != NULL && pMA4 != NULL) {
    pMa1 = [NSString stringWithUTF8String:pMA1];
    pMa2 = [NSString stringWithUTF8String:pMA2];
    pMa3 = [NSString stringWithUTF8String:pMA3];
    pMa4 = [NSString stringWithUTF8String:pMA4];
  }
#warning ///////
    //修改均线
    QUOTELV_K_INDEX *kIndex = g_pQuoteOper->KIndex;
    kIndex->PAVG_N1 = atoi(pMA1);
    kIndex->PAVG_N2 = atoi(pMA2);
    kIndex->PAVG_N3 = atoi(pMA3);
    kIndex->PAVG_N4 = atoi(pMA4);
  
  pMaField0.text = pMa1;
  pMaField1.text = pMa2;
  pMaField2.text = pMa3;
  pMaField3.text = pMa4;
}

-(void)saveData{
  ReadConfig();
  NSString* pMa1 = pMaField0.text;
  NSString* pMa2 = pMaField1.text;
  NSString* pMa3 = pMaField2.text;
  NSString* pMa4 = pMaField3.text;
  
  AddConfig(CONFIG_MA1,[pMa1 UTF8String]);
  AddConfig(CONFIG_MA2,[pMa2 UTF8String]);
  AddConfig(CONFIG_MA3,[pMa3 UTF8String]);
  AddConfig(CONFIG_MA4,[pMa4 UTF8String]);
  SaveConfig();
}
@end
